﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data.OleDb;
using System.Data;

namespace PulseApp
{
    /// <summary>
    /// Interaction logic for Page1.xaml
    /// </summary>
    public partial class Page1 : Page
    {

        Person person;
        bool selectedPersonChanged = false;

        public Page1()
        {
            InitializeComponent();

            selectedPersonChanged = false;
            myDataSet = (pulseDataSet)(this.FindResource("pulseDataSet"));
            myAdapter = new pulseDataSetTableAdapters.personTableAdapter();
            myAdapter.Fill(myDataSet.person);
            System.Windows.Data.CollectionViewSource personViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("personViewSource")));
            personViewSource.View.MoveCurrentToFirst();
            if(CommonResources.Instance.getRole() == CommonResources.ROLE_REGULAR)
            {
                personViewSource.Source = myDataSet.person.Select("ID = " + CommonResources.Instance.getPerson().getId());
                addBbutton.Visibility = System.Windows.Visibility.Hidden;
                addBbuttonTransform.Visibility = System.Windows.Visibility.Hidden;
            }
            Person aPerson = CommonResources.Instance.getPerson();
            if (aPerson != null)
            {
                this.person = aPerson;
                //setPersonInfo(person);
                
            }
        }

        private void setPersonInfo(Person person)
        {

            this.lastNameBox.Text = person.LastName;
            this.firstNameBox.Text = person.FirstName;
            this.sexBox.Text = person.Sex;
            this.ageBox.Text = "" + person.Age;
            this.heightBox.Text = "" + person.Height;
            this.weightBox.Text = "" + person.Weight;
            this.hbpBox.Text = "" + person.Hbp;
            this.lbpBox.Text = "" + person.Lbp;
            this.streetBox.Text = person.Street;
            this.cityBox.Text = person.City;
            this.provinceBox.Text = person.Province;
            this.countryBox.Text = person.Country;
            this.postCodeBox.Text = person.PostCode;
            this.phoneBox.Text = person.Phone;
            this.faxBox.Text = "";
            this.emailBox.Text = person.Email;
            this.UsernameBox.Text = person.Username;
            this.PasswordBox.Text = person.Password;
        }

        private bool readPersonInfo()
        {
            bool sex = (this.sexBox.SelectedIndex == 0);
            int age = 0;
            int height = 0;
            int weight = 0;
            int hbp = 0;
            int lbp = 0;
            int id = 0;
            string message = "";
            bool error = false;
            try
            {
                age = int.Parse(ageBox.Text.Trim());
            }
            catch (Exception ex)
            {
                message += "The age is not a number. Please input the correct age!\r\n";
                error = true;
            }
            try
            {
                height = int.Parse(heightBox.Text.Trim());
            }
            catch (Exception ex)
            {
                message += "The height is not a number. Please input the correct height!\r\n";
                error = true;
            }
            try
            {
                weight = int.Parse(weightBox.Text.Trim());
            }
            catch (Exception ex)
            {
                message += "The weight is not a number. Please input the correct weight!\r\n";
                error = true;
            }
            try
            {
                hbp = int.Parse(hbpBox.Text.Trim());
            }
            catch (Exception ex)
            {
                message += "The hbp is not a number. Please input the correct hbp!\r\n";
                error = true;
            }
            try
            {
                lbp = int.Parse(lbpBox.Text.Trim());
            }
            catch (Exception ex)
            {
                message += "The lbp is not a number. Please input the correct lbp!\r\n";
                error = true;
            }
            try
            {
                id = int.Parse(personListBox.SelectedValue.ToString());
                Console.WriteLine("id is " + id);
            }
            catch (Exception ex)
            {
                message += "user id is not valid\r\nThe Selected Value is" + personListBox.SelectedValue.ToString();
                error = true;
            }
            if (!error)
            {
                person = new Person(id, lastNameBox.Text.Trim(), firstNameBox.Text.Trim(), sexBox.Text.Trim(), age, height, weight, hbp, lbp,
                    streetBox.Text.Trim(), cityBox.Text.Trim(), provinceBox.Text.Trim(), countryBox.Text.Trim(),
                    postCodeBox.Text.Trim(), phoneBox.Text.Trim(), faxBox.Text.Trim(), emailBox.Text.Trim());

            }
            else
            {
                MessageBox.Show(message);
            }
            return error;
        }

        private void Page_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {

            if (!readPersonInfo())
            {
                if (CommonResources.Instance.getPerson() == null || this.selectedPersonChanged)
                {
                    CommonResources.Instance.setPerson(person);
                }
                NavigationService.Navigate(new Uri("Index.xaml", UriKind.Relative));
            }
        }


        private void personListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            selectedPersonChanged = true;
        }

        private void clearTexts()
        {
            this.lastNameBox.Clear();
            this.firstNameBox.Clear();
            this.ageBox.Clear();
        }

        private void addBbutton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("Page5.xaml", UriKind.Relative));
        }

        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            
        }

        private void monitorButton_Click(object sender, RoutedEventArgs e)
        {
            if (!readPersonInfo())
            {
                if (CommonResources.Instance.getPerson() == null || this.selectedPersonChanged)
                {
                    CommonResources.Instance.setPerson(person);
                }
                NavigationService.Navigate(new Uri("Page2.xaml", UriKind.Relative));
            }
        }



        
    }
}
